package com.blog.ctrl;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.bean.Blog;
import com.blog.service.BlogService;

@Lazy(true)
@Controller
@RequestMapping("/blog")
public class BlogCtrl {
	@Autowired
	private BlogService blogService;
	
	@ResponseBody
	@RequestMapping("/addBlog.do")
	public String addBlog(Blog blog){
		if(blog == null){
			System.out.println("对象为空");
		}else{
			blog.setUserId(1);
			blog.setCreateTime(new Date(System.currentTimeMillis()));
			blog.setViewCount(0);
			blog.setDelStatus(0);
			blog.setUpdateTime(new Date(System.currentTimeMillis()));
			System.out.println(blog.getTitle());
			blogService.addBlog(blog);
		}
		return "";
	}
	
	@RequestMapping("/getBlogByType.do")
	public ModelAndView getBlogByType(HttpServletRequest request){
		String blogType = request.getParameter("blogType");
		List<Blog> blogs = blogService.getBlogByType(blogType);
		ModelAndView v = new ModelAndView();
		v.addObject("blogs", blogs);
		v.setViewName("blogList");
		return v;
	}
	
	@RequestMapping("/getBlogId.do")
	public ModelAndView getBlogById(int id){
		Blog blog = blogService.getBlogById(id);
		ModelAndView v = new ModelAndView();
		v.setViewName("blog/seeBlog");
		v.addObject("blog", blog);
		return v;
	}
	
	@RequestMapping("/index.do")
	public ModelAndView holleWord(){
		List<Blog> blogs = blogService.holleWord();
		ModelAndView v = new ModelAndView();
		v.addObject("blogs", blogs);
		v.setViewName("index");
		return v;
	}
	
}
